home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pgm / pgmenhan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  3.3 KB  |  125 lines

  1. /* pgmenhance.c - edge-enhance a portable graymap
  2. **
  3. ** Copyright (C) 1989, 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pgm.h"
  14.  
  15. void
  16. main( argc, argv )
  17. int argc;
  18. char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     gray* prevrow;
  22.     gray* thisrow;
  23.     gray* nextrow;
  24.     gray* temprow;
  25.     gray* newrow;
  26.     register gray* ngP;
  27.     int argn, n, rows, cols, row, col;
  28.     float phi, omphi;
  29.     gray maxval;
  30.     long sum, newval;
  31.     int format;
  32.     char* usage = "[-N] [pgmfile]  ( 1 <= N <= 9, default = 9 )";
  33.  
  34.     pgm_init( &argc, argv );
  35.  
  36.     argn = 1;
  37.     n = 9;
  38.  
  39.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  40.     {
  41.     if ( sscanf( &(argv[argn][1]), "%d", &n ) != 1 )
  42.         pm_usage( usage );
  43.     if ( n < 1 || n > 9 )
  44.         pm_usage( usage );
  45.     ++argn;
  46.     }
  47.  
  48.     if ( argn != argc )
  49.     {
  50.     ifp = pm_openr( argv[argn] );
  51.     ++argn;
  52.     }
  53.     else
  54.     ifp = stdin;
  55.  
  56.     if ( argn != argc )
  57.     pm_usage( usage );
  58.  
  59.     pgm_pbmmaxval = 255;    /* use a larger value for better (?) results */
  60.     pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  61.     prevrow = pgm_allocrow( cols );
  62.     thisrow = pgm_allocrow( cols );
  63.     nextrow = pgm_allocrow( cols );
  64.  
  65.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  66.     newrow = pgm_allocrow( cols );
  67.  
  68.     /* The edge enhancing technique is taken from Philip R. Thompson's "xim"
  69.     ** program, which in turn took it from section 6 of "Digital Halftones by
  70.     ** Dot Diffusion", D. E. Knuth, ACM Transaction on Graphics Vol. 6, No. 4,
  71.     ** October 1987, which in turn got it from two 1976 papers by J. F. Jarvis
  72.     ** et. al.
  73.     */
  74.     phi = n / 10.0;
  75.     omphi = 1.0 - phi;
  76.  
  77.     /* First row. */
  78.     pgm_readpgmrow( ifp, thisrow, cols, maxval, format );
  79.     pgm_writepgmrow( stdout, thisrow, cols, maxval, 0 );
  80.     pgm_readpgmrow( ifp, nextrow, cols, maxval, format );
  81.  
  82.     /* Other rows. */
  83.     for ( row = 1; row < rows - 1; row++ )
  84.     {
  85.     temprow = prevrow;
  86.     prevrow = thisrow;
  87.     thisrow = nextrow;
  88.     nextrow = temprow;
  89.     pgm_readpgmrow( ifp, nextrow, cols, maxval, format );
  90.  
  91.     ngP = newrow;
  92.     *ngP = thisrow[0];
  93.     ngP++;
  94.     for ( col = 1; col < cols - 1; col++, ngP++ )
  95.         {
  96.         /* Compute the sum of the neighborhood. */
  97.         sum =
  98.         (long) prevrow[col-1] + (long) prevrow[col] +
  99.         (long) prevrow[col+1] +
  100.         (long) thisrow[col-1] + (long) thisrow[col] +
  101.         (long) thisrow[col+1] +
  102.         (long) nextrow[col-1] + (long) nextrow[col] +
  103.         (long) nextrow[col+1];
  104.         /* Now figure new value. */
  105.         newval = ( ( thisrow[col] - phi * sum / 9 ) / omphi + 0.5 );
  106.         if ( newval < 0 )
  107.         *ngP = 0;
  108.         else if ( newval > maxval )
  109.         *ngP = maxval;
  110.         else
  111.         *ngP = newval;
  112.         }
  113.     *ngP = thisrow[cols - 1];
  114.     pgm_writepgmrow( stdout, newrow, cols, maxval, 0 );
  115.     }
  116.     pm_close( ifp );
  117.  
  118.     /* Last row. */
  119.     pgm_writepgmrow( stdout, nextrow, cols, maxval, 0 );
  120.  
  121.     pm_close( stdout );
  122.  
  123.     exit( 0 );
  124.     }
  125.